package com.zjdiepu.www.manage.api.cw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.cw.ICwRemitAdjustbSubService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustbSub;
import com.zjdiepu.www.model.cw.ext.CwRemitAdjustbSubExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.ext.YwBusinessLoanExt;

/**
 * cw_垫款调整B子表Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年09月07日 15时57分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "CwRemitAdjustbSubController", produces="application/json;charset=UTF-8")
@Api(tags = {"cw_垫款调整B子表"}, produces="application/json;charset=UTF-8")
public class CwRemitAdjustbSubController extends BaseController {
    @Resource
    private ICwRemitAdjustbSubService cwRemitAdjustbSubService;
    
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;

    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增cw_垫款调整B子表 <br/>
     * @param cwRemitAdjustbSub
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute CwRemitAdjustbSub cwRemitAdjustbSub) {
    	AbstractSysUser currentUser = getCurrentUser();
    	cwRemitAdjustbSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustbSub.setModifierName(currentUser.getName());
        cwRemitAdjustbSubService.saveUpdateAndDelete(cwRemitAdjustbSub, OperateType.add);
        return ApiResponse.success(true);
    }
    
    @ApiOperation(value = "新增cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ApiResponse insert(@Validated(Add.class) @ModelAttribute CwRemitAdjustbSub cwRemitAdjustbSub) {
    	AbstractSysUser currentUser = getCurrentUser();
    	cwRemitAdjustbSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustbSub.setModifierName(currentUser.getName());
    	
    	YwBusinessLoanExt ywBusinessLoanExt = new YwBusinessLoanExt();
		ywBusinessLoanExt.setBusiId(cwRemitAdjustbSub.getBusiId());
		ywBusinessLoanExt.setCarId(cwRemitAdjustbSub.getCarId());
		ywBusinessLoanExt.setBaseWhere();
		ywBusinessLoanExt.setRows(1);
		List<YwBusinessLoan> oldYwBusinessLoans = ywBusinessLoanService.selectByExt(ywBusinessLoanExt);
		if(oldYwBusinessLoans!=null && oldYwBusinessLoans.size()>0){
			YwBusinessLoan ywBusinessLoanOld= new YwBusinessLoan();
			cwRemitAdjustbSub.setAmountMonthlyOld(ywBusinessLoanOld.getAmountMonthly());
			cwRemitAdjustbSub.setCarEstimatePriceOld(ywBusinessLoanOld.getCarEstimatePrice());
			cwRemitAdjustbSub.setCarPriceOld(ywBusinessLoanOld.getCarPrice());
			cwRemitAdjustbSub.setFirstMonthAmountOld(ywBusinessLoanOld.getFirstMonthAmount());
			cwRemitAdjustbSub.setFirstMonthFee2Old(ywBusinessLoanOld.getFirstMonthFee2());
			cwRemitAdjustbSub.setFirstMonthFeeOld(ywBusinessLoanOld.getFirstMonthFee());
			cwRemitAdjustbSub.setFirstMonthTotalAmountOld(ywBusinessLoanOld.getFirstMonthTotalAmount());
			cwRemitAdjustbSub.setTotalRepaymentAmountOld(ywBusinessLoanOld.getTotalRepaymentAmount());
			cwRemitAdjustbSub.setTotalRepaymentProportionOld(ywBusinessLoanOld.getTotalRepaymentProportion());
			cwRemitAdjustbSub.setMonthlyPrincipalAmountOld(ywBusinessLoanOld.getMonthlyPrincipalAmount());
			cwRemitAdjustbSub.setMonthlyFeeOld(ywBusinessLoanOld.getMonthlyFee());
			cwRemitAdjustbSub.setMonthlyFee2Old(ywBusinessLoanOld.getMonthlyFee2());
			cwRemitAdjustbSub.setLoanMonthOld(ywBusinessLoanOld.getLoanMonth());
		}
        cwRemitAdjustbSubService.saveUpdateAndDelete(cwRemitAdjustbSub, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除cw_垫款调整B子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustbSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用cw_垫款调整B子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustbSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用cw_垫款调整B子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	cwRemitAdjustbSubService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改cw_垫款调整B子表 <br/>
     * @param cwRemitAdjustbSub
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改cw_垫款调整B子表", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute CwRemitAdjustbSub cwRemitAdjustbSub) {
    	AbstractSysUser currentUser = getCurrentUser();
    	cwRemitAdjustbSub.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	cwRemitAdjustbSub.setModifierName(currentUser.getName());
    	cwRemitAdjustbSubService.saveUpdateAndDelete(cwRemitAdjustbSub, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询cw_垫款调整B子表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询cw_垫款调整B子表", response = CwRemitAdjustbSub.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       CwRemitAdjustbSub cwRemitAdjustbSub = cwRemitAdjustbSubService.selectById(id);
       if(null == cwRemitAdjustbSub) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(cwRemitAdjustbSub);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询cw_垫款调整B子表 <br/>
     * @param cwRemitAdjustbSubExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询cw_垫款调整B子表",response = CwRemitAdjustbSub.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute CwRemitAdjustbSubExt cwRemitAdjustbSubExt){
        cwRemitAdjustbSubExt.notDeleted();
        PageVo page = getPage(cwRemitAdjustbSubExt);
        int records = cwRemitAdjustbSubService.countByExt(cwRemitAdjustbSubExt);
        page.setRecords(records);
        if (records > 0) {
            List<CwRemitAdjustbSub> dpBaseDataDictionaries = cwRemitAdjustbSubService.selectByExt(cwRemitAdjustbSubExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
